package com.xuecheng.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.service.FindPasswordService;
import com.xuecheng.ucenter.mapper.XcCompanyMapper;
import com.xuecheng.ucenter.mapper.XcCompanyUserMapper;
import com.xuecheng.ucenter.mapper.XcUserMapper;
import com.xuecheng.ucenter.model.dto.FindPasswordDto;
import com.xuecheng.ucenter.model.po.XcCompany;
import com.xuecheng.ucenter.model.po.XcCompanyUser;
import com.xuecheng.ucenter.model.po.XcUser;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 陈竞超
 * @version 1.0
 * @qq 2250537249
 */
@Service
@ComponentScan(value = "com.xuecheng.*")
@Log4j2
public class FindPasswordServiceImpl implements FindPasswordService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private XcCompanyMapper xcCompanyMapper;
    @Autowired
    private XcCompanyUserMapper xcCompanyUserMapper;
    @Autowired
    private XcUserMapper xcUserMapper;
    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    @Transactional
    public FindPasswordDto ByPhone(FindPasswordDto findPasswordDto) {
        if (findPasswordDto == null){
            throw new XueChengPlusException("输入信息为空");
        }
        String checkcode = redisTemplate.opsForValue().get("checkcode").toString();
        System.out.println(checkcode);
        if (checkcode.equals(findPasswordDto.getCheckcode())) {
            LambdaQueryWrapper<XcUser> xcUserWrapper = new LambdaQueryWrapper<>();
            xcUserWrapper.eq(XcUser::getCellphone,findPasswordDto.getCellphone());
            XcUser xcUser_after = new XcUser();
            String encode = passwordEncoder.encode(findPasswordDto.getPassword());
            xcUser_after.setPassword(encode);
            xcUserMapper.update(xcUser_after,xcUserWrapper);
            redisTemplate.delete("checkcode");
            return findPasswordDto;
        }else {
            //报错
            throw new XueChengPlusException("验证错误");
        }
    }

    @Override
    public void ByEmail(String code) {

    }
}
